﻿// JavaScript Document
// itea 2009-07-22

$(document).ready(function(){
	dwr.engine.setTimeout(5000);
	dwr.engine.setErrorHandler(function(msg){
		page.setServerStatus(msg);
	});
	dwr.engine.setWarningHandler(function(msg){
		page.setServerStatus(msg);
	});
	NBoard.init(['col_1', 'col_2']);
	page.timeBeatId = window.setInterval('page.timeBeat()', 1000);
	
	if($.browser.msie)
		page.setMsg('使用IE浏览器无法取得良好的显示效果');
	
});

// Page
var page = {
	timeBeatId: null,
	beat: 0,
	clientConfig: {
		checkNewsInterval: 60,
		commentsUpdateInterval: 30,
		readNewsLeftTime: 180
	},
	init: function(options, onready) {
		
		if(onready && onready instanceof Function)
			onready.call();
	},
	setClientConfig: function(cc) {
		if(cc) $.extend(this.clientConfig, cc);
	},
	postNews: function() {
		window.open('postNews.html', 'bigNews_postNews_html', '', false);
	},
	setServerStatus: function(s) {
		switch(s) {
		case 'Timeout':
		case 'Not Found':
		case 'No data received from server':
			$('#server_status').removeClass('ui-state-default')
				.addClass('ui-state-error');
			$('#s_status').text('断开');
			break;
		case 'Ok':
			$('#server_status').removeClass('ui-state-error')
				.addClass('ui-state-default');
			$('#s_status').text('良好');
			break;
		default:
			if(s) this.setMsg(s);
		}
	},
	setMsg: function(s) {
		if(s) {// 显示过的msg不再显示第二遍
			$('#msg').html('<span class="ui-icon ui-icon-info" ></span>'+ s).show();
		} else
			$('#msg').hide('slow');
	},
	setAlert: function(s) {
		if(s)
			$('#msg').html('<span class="ui-icon ui-icon-alert" ></span>'+ s).show();
		else
			$('#msg').hide('slow');
	},
	timeBeat: function() {
		if(this.beat++ % this.clientConfig.checkNewsInterval == 1) {
			NBoard.closeExpiredNBoxes();
			NBoard.checkNews(function(){
				NBoard.refresh();
			});
		} else if(this.beat % this.clientConfig.commentsUpdateInterval == 1) { // 更新内容(评论，时间等)
			NBoard.refresh();
		}
	}
};

var NBoard = {
	maxNewsPerNStream: 3,
	nstreams: new Array(),
	init: function(nsTagIds, maxNewsPerNStream) {
		this.maxNewsPerNStream = maxNewsPerNStream || 3;
		for(var i=0; i<nsTagIds.length; i++)
			this.nstreams.push(new NStream(nsTagIds[i], this.maxNewsPerNStream).clear());
		return this;
	},
	// execute callback after check and show news
	checkNews: function(callback) {
		var max = 0;
		for(var i=0, j=this.nstreams.length; i<j; i++)
			max += this.nstreams[i].capacityLeft();
		if(max > 0)
			newsDwr.userReadNews(max, function(sresult){
				var newsList = sresult.result;
				page.setServerStatus('Ok');
				for(var i=0; i<newsList.length; i++) {
					NBoard.getNextNStream().insertNews(newsList[i]);
				}
				if(callback)
					callback.call(null, newsList);
				page.setClientConfig(sresult.clientConfig);
			});
	},
	refresh: function() {
		try {
			DWREngine.beginBatch();
			for(var i=0, j=this.nstreams.length; i<j; i++)
				this.nstreams[i].refresh();
		} catch(e) {
			page.setAlert(e);
		} finally {
			DWREngine.endBatch();
		}
	},
	// 取得下一个适合加入news的NStream
	getNextNStream: function() {
		var s = this.nstreams.shift();
		this.nstreams.push(s);
		for(var i=1, j=this.nstreams.length; i<j; i++) {
			var ns = this.nstreams.shift();
			this.nstreams.push(ns);
			if(ns.capacityLeft() > s.capacityLeft())
				s = ns;
		}
		return s;
	},
	closeNBox: function(id) {
		for(var i=0, j=this.nstreams.length; i<j; i++)
			this.nstreams[i].closeNBox(id);
	},
	closeExpiredNBoxes: function() {
		for(var i=0, j=this.nstreams.length; i<j; i++)
			this.nstreams[i].closeExpiredNBoxes();
	}
};

var NStream = function(tagId, maxNews) {
	this.tagId = tagId;
	this.maxNews = maxNews || 3;
	this._tagId = '#'+ tagId;
	this.nboxes = new Array();
};

$.extend(NStream.prototype, {
	clear: function() {
		$(this._tagId).children('.nbox').remove();
		return this;
	},
	insertNews: function(news) {
		var nbox = new NNews(news);
		$(this._tagId).prepend(nbox.genHtml());
		this.nboxes.push(nbox.render());
		return this;
	},
	insertBulletin: function(bulletin) {
		var nbox = new NBulletin(bulletin);
		$(this._tagId).prepend(nbox.genHtml());
		this.nboxes.push(nbox.render());
		return this;
	},
	size: function() {
		return this.nboxes.length;
	},
	capacityLeft: function() {
		return this.maxNews - this.nboxes.length;
	},
	refresh: function() {
		for(var i=0, j=this.nboxes.length; i<j ;i++)
			this.nboxes[i].refresh();
		return this;
	},
	closeNBox: function(id) {
		for(var i=0, j=this.nboxes.length; i<j ;i++)
			if(id == this.nboxes[i].id) {
				var nbox = this.nboxes.splice(i, 1); // remove this nbox
				nbox[0].close();
				break;
			}
	},
	closeExpiredNBoxes: function() {
		for(var i=0; i<this.nboxes.length ;i++)
			if(this.nboxes[i].isExpired()) {
				var nbox = this.nboxes.splice(i, 1); // remove this nbox
				nbox[0].close();
			}
	}
});